<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
    <script>
        // 获取cooKie的函数封装
        // 
      document.cookie="sex=1";
      document.cookie="username=xiaosun";
      document.cookie="age=88"
    function getCookie(key){ //key--username
        var cookies=document.cookie; //获取所有的cooKie
        console.log(cookies)//sex=1; username=xiaosun; age=88
        var arr=cookies.split("; ")//
        console.log(arr) //['sex=1', 'username=xiaosun', 'age=88']
        for(var i=0;i<arr.length;i++){
            var cookiearr=arr[i].split("=")
            console.log(cookiearr)//['sex',1]  ['username','xiaosun'] ['age',88]
            if(key==cookiearr[0]){ //传入的参数 和cookie中的0下标对比成功
              return cookiearr[1]//返回1的下标
            }
        }
    }
    var myname=getCookie("age")
    console.log(myname)//xiaosun

    </script>
</body>
</html>